package com.zr.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.zr.dao.AddNumberDao;
import com.zr.model.Content;
import com.zr.util.GetJDBCUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
 * 增加浏览次数的数据库操作的接口
 * @author Administrator
 *
 */
public class AddNumberDaoImpl implements AddNumberDao{

	/**
	 * 增加浏览次数的数据库操作的实例方法
	 */
	GetJDBCUtil gt = new GetJDBCUtil();
	
	@Override
	public void setNumberById(int cid) {
		Connection con = gt.getJDBCConnection();
		int number = this.getNumberById(cid)+1;
		StringBuffer sql = new StringBuffer(""); 
		sql.append("UPDATE e_content ");
		sql.append("SET e_content.cnumber=? ");
		sql.append("WHERE e_content.cid=? ");
		
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, number);
			pst.setInt(2, cid);
			
			int rs = pst.executeUpdate();
			
			pst.close();
			con.close();
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	@Override
	public int getNumberById(int cid) {
		Connection con = gt.getJDBCConnection();
		int number = 0;
		
		StringBuffer sql = new StringBuffer(""); 
		sql.append("SELECT e_content.cnumber ");
		sql.append("FROM e_content ");
		sql.append("WHERE e_content.cid=? ");
		
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, cid);
			ResultSet rs = pst.executeQuery();
			
			while(rs.next()){
				number = rs.getInt("cnumber");
			}
			
			rs.close();
			pst.close();
			con.close();
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return number;
	}

	@Override
	public void setBrowse(int cid) {
		Connection con = gt.getJDBCConnection();
		
		StringBuffer sql = new StringBuffer(""); 
		sql.append("INSERT INTO e_browse(e_browse.cid) ");
		sql.append("VALUES (?) ");
		
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, cid);
			
			int rs = pst.executeUpdate();
			
			pst.close();
			con.close();
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	
}
